package util;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Objects;
import java.util.TimeZone;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;

public class FtpUtil {
    private final FTPClient ftpClient = new FTPClient();
    private final String strIp = "47.108.198.17";
    private final int intPort = 39029;
    private final String user = "hustoj";
    private final String password = "dZHSEjPGdnZephEd";
    private static final FtpUtil FTP_UTIL = new FtpUtil();

    public static FtpUtil getInstance() {
        return FTP_UTIL;
    }

    private FtpUtil() {
    }

    public boolean ftpLogin() {
        boolean isLogin = false;
        FTPClientConfig ftpClientConfig = new FTPClientConfig();
        ftpClientConfig.setServerTimeZoneId(TimeZone.getDefault().getID());
        this.ftpClient.setControlEncoding("utf-8");
        this.ftpClient.configure(ftpClientConfig);

        try {

            ftpClient.connect("47.108.198.17", 39029);
            if (ftpClient.login("hustoj", "dZHSEjPGdnZephEd")) {
                System.out.println("连接成功");
            }

            this.ftpClient.enterLocalPassiveMode();
            this.ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            isLogin = true;
        } catch (Exception var4) {
            var4.printStackTrace();
        }

        this.ftpClient.setBufferSize(2048);
        this.ftpClient.setDataTimeout(30000);
        return isLogin;
    }

    public void ftpLogOut() {
        if (null != this.ftpClient && this.ftpClient.isConnected()) {
            try {
                this.ftpClient.logout();
            } catch (IOException var10) {
                var10.printStackTrace();
            } finally {
                try {
                    this.ftpClient.disconnect();
                } catch (IOException var9) {
                    var9.printStackTrace();
                }

            }
        }

    }

    public boolean uploadFile(String pathname, String fileName, File originfile) {
        boolean flag = false;
        FileInputStream inputStream = null;

        try {
            System.out.println("开始上传文件");
            inputStream = new FileInputStream(originfile);
            this.makeDir(pathname);
            this.ftpClient.enterLocalPassiveMode();
            this.ftpClient.storeFile(fileName, inputStream);
            inputStream.close();
            flag = true;
            System.out.println("上传文件成功");
            ftpClient.changeWorkingDirectory("/");

            return flag;
        } catch (Exception e) {
            System.out.println("上传文件失败");
            e.printStackTrace();
            return false;
        }
    }

    public boolean makeDir(String pathname) {
        try {
            this.ftpClient.makeDirectory(pathname);
        }
        catch (Exception ignored){
        }
        try {
            this.ftpClient.changeWorkingDirectory(pathname);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  true;
    }

    public void  deleteDir(String pathname){
        try {
            ftpClient.removeDirectory(pathname);
        } catch (IOException ignored) {

        }
    }
}

